package com.nulldev.util.os.notifications;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.LinkedList;

import javax.swing.JDialog;
import java.awt.BorderLayout;

import com.nulldev.util.FinalBypass;
import com.nulldev.util.java.swing.SwingUtil;
import com.nulldev.util.java.swing.canvas.PaintMethod;
import com.nulldev.util.java.swing.canvas.SmoothCanvas;
import com.nulldev.util.java.swing.extSwing.notifications.Notification;
import com.nulldev.util.java.swing.font.FontManager;
import com.nulldev.util.java.utils.color.ColorUtil;
import com.nulldev.util.logging.LoggerFactory;

public class JNotification extends JDialog {

	private final LinkedList<Notification> note = new LinkedList<Notification>();
	
	/**
	 * Launch the application.
	 * @return 
	 */
	public static JNotification main() {
		if (true) {
			LoggerFactory.getLogger().error("main() -> JNotification is unsupported, returning null.");
			return null;
		}
		final FinalBypass<JNotification> out = new FinalBypass<JNotification>(null);
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JNotification dialog = new JNotification();
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					out.setObject(dialog);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		while (!out.isNull()) {
			try {
				Thread.sleep(50L);
			} catch (InterruptedException e) {
			}
		}
		return out.getObject();
	}

	/**
	 * Create the dialog.
	 */
	public JNotification() {
		setBounds(1, 1, 237, 81);
		setUndecorated(true);
		setFocusable(false);
		setFocusableWindowState(false);
		setFocusCycleRoot(false);
		setAutoRequestFocus(false);
		enableInputMethods(false);
		SwingUtil.setOpaque(this, false);
		getContentPane().setLayout(new BorderLayout(0, 0));
		setBackground(ColorUtil.TRANSPARENT);
		
		final Font std = FontManager.getHighway(16);
		final SmoothCanvas panel = new SmoothCanvas() {
			
			@Override
			public void drawCanvas(final Graphics2D g2d) {
				g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				g2d.setFont(std);
				
				if (!JNotification.this.note.isEmpty()) {
					final Notification j = JNotification.this.note.getFirst();
					if (j.getTitle().equals("")) {
						g2d.drawString(j.getText(), 5, 15);
					}else {
						g2d.drawString(j.getTitle(), 5, 15);
						g2d.drawString(j.getText(), 5, 45);
					}
					if (j.getTimer().hasReached(3500)) {
						JNotification.this.note.remove(j);
						j.getTimer().stop();
					}
				}
			}
		};
		panel.setBackground(ColorUtil.TRANSPARENT);
		panel.setFPS(24);
		panel.usePaintMethod(PaintMethod.REPAINT_DELAYED);
		getContentPane().add(panel, BorderLayout.CENTER);

	}

}
